package Hangman;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import net.miginfocom.swing.MigLayout;
/**
 * Settings class, handles all settings in the game together with FileLoader.
 * @author Robin E, Jakob L, Isac A, Cristian O, Eric P, Tobias W. 
 *
 */

@SuppressWarnings("serial")
public class Settings extends JPanel {

	private JLabel headLineLabel;
	private JLabel themeLabel;
	private JLabel numberPlayersLabel;
	private JLabel difficultyLabel;
	
	private JLabel acceptSettingsLabel;
	private JLabel cancelSettingsLabel;
	
	private JComboBox<String> themes;
	private String[] strings = {"Animals", "Movies", "Games"};
	
	private ButtonGroup players;
	private JRadioButton playerOne;
	private JRadioButton playerTwo;
	private JRadioButton playerThree;
	
	private ButtonGroup difficulties;
	private JRadioButton easyDifficulty;
	private JRadioButton mediumDifficulty;
	private JRadioButton hardDifficulty;
	private JRadioButton extremeDifficulty;
	
	private JPanel settingsPanel;
	private JPanel headPanel;
	private JPanel btnPanel;
	//starts without a song
	private Sound sound= new Sound(null);

		
	/**
	 * Settings Contructor, initializes all components in the class.
	 * Also adds all components to their respective JPanel.
	 */
	Settings()
	{
		setLayout(new MigLayout());
		setBackground(Color.white);
		settingsPanel = new JPanel();
		headPanel = new JPanel();
		headPanel.setBackground(Color.white);
		settingsPanel.setLayout(new MigLayout());
		settingsPanel.setBackground(Color.white);
		headPanel.setLayout(new MigLayout());
		btnPanel = new JPanel();
		btnPanel.setBackground(Color.white);
		btnPanel.setLayout(new MigLayout());
		
		Font largsHeadingFont = new Font("Arial", Font.BOLD, 40);
		Font mediumFont = new Font("Arial", Font.BOLD, 20);
		
		headLineLabel = new JLabel("Settings");
		headLineLabel.setFont(largsHeadingFont);
		
		themeLabel = new JLabel("Theme");
		themeLabel.setFont(mediumFont);
		
		numberPlayersLabel = new JLabel("Players");
		numberPlayersLabel.setFont(mediumFont);
		
		difficultyLabel = new JLabel("Difficulty");
		difficultyLabel.setFont(mediumFont);
		
		acceptSettingsLabel = new JLabel("Accept");
		acceptSettingsLabel.setFont(mediumFont);
		acceptSettingsLabel.addMouseListener(new applySettingsListener());
		cancelSettingsLabel = new JLabel("Cancel");
		cancelSettingsLabel.setFont(mediumFont);
		cancelSettingsLabel.addMouseListener(new cancelListener());
		
		themes = new JComboBox<String>();
		themes.setBackground(Color.white);
		
		for(String i : strings)
		{
			themes.addItem(i);
		}
		
		themes.setSelectedIndex(1);
		
		players = new ButtonGroup();
		playerOne = new JRadioButton("1");
		playerOne.setBackground(Color.white);
		playerTwo = new JRadioButton("2");
		playerTwo.setBackground(Color.white);
		playerThree = new JRadioButton("3");
		playerThree.setBackground(Color.white);
		
		difficulties = new ButtonGroup();
		easyDifficulty = new JRadioButton("Easy(10)");
		easyDifficulty.setBackground(Color.white);
		mediumDifficulty = new JRadioButton("Medium(7)");
		mediumDifficulty.setBackground(Color.white);
		hardDifficulty = new JRadioButton("Hard(5)");
		hardDifficulty.setBackground(Color.white);
		extremeDifficulty = new JRadioButton("Extreme(2)");
		extremeDifficulty.setBackground(Color.white);
		
		players.add(playerOne);
		players.add(playerTwo);
		players.add(playerThree);
		
		difficulties.add(easyDifficulty);
		difficulties.add(mediumDifficulty);
		difficulties.add(hardDifficulty);
		difficulties.add(extremeDifficulty);
		
		headPanel.add(new JLabel(""), "wrap 270");
		headPanel.add(headLineLabel, "gapbefore 380");
		headPanel.add(new JLabel(""), "wrap");
		
		settingsPanel.add(themeLabel);
		settingsPanel.add(themes);
		settingsPanel.add(new JLabel(""), "wrap 20");
		
		settingsPanel.add(numberPlayersLabel);
		settingsPanel.add(playerOne);
		settingsPanel.add(playerTwo);
		settingsPanel.add(playerThree);
		settingsPanel.add(new JLabel(""), "wrap");
		
		settingsPanel.add(difficultyLabel);
		settingsPanel.add(easyDifficulty);
		settingsPanel.add(mediumDifficulty);
		settingsPanel.add(hardDifficulty);
		settingsPanel.add(extremeDifficulty);
		settingsPanel.add(new JLabel(""), "wrap 30");
		
		btnPanel.add(acceptSettingsLabel, "gapbefore 370");
		btnPanel.add(cancelSettingsLabel, "gapbefore 100");
		
	}
	
	/**
	 * init() adds all JPanels to the frame and show the window.
	 */
	public void init()
	{
		add(headPanel, "wrap");
		add(settingsPanel, "gapbefore 300");
		add(new JLabel(""), "wrap");
		add(btnPanel);
		showWindow();
	}
	/**
	 * hideWindow() is a simple method to hide this JPanel
	 */
	public void hideWindow()
	{
		setVisible(false);
	}
	/**
	 * showWindow() is a simple method to show the JPanel and set the size of the JPanel.
	 */
	public void showWindow()
	{
		setSize(1000, 800);
		setVisible(true);
	}
	
	/**
	 * cancelListener is an inner class listener which holds two states which changes
	 * the mouse cursor as well as asking the player whether he/she is sure to cancel settings.
	 * Used by the JLabel cancelSettings
	 * @author Robin E, Jakob L, Isac A, Cristian O, Eric P, Tobias W. 
	 *
	 */
	public class cancelListener extends MouseAdapter
	{
		public void mouseEntered(MouseEvent mentered)
		{
			Cursor c = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
			setCursor(c);
		}
		public void mouseExited(MouseEvent mexit)
		{
			Cursor c = Cursor.getDefaultCursor();
			setCursor(c);
		}
		public void mouseReleased(MouseEvent mreleased)
		{
			sound.beepPool("hangman-sounds/beep01.wav");
			int choice = JOptionPane.showConfirmDialog(null, "Are you sure you wish to cancel settings?", 
					"Warning", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
			
			if(choice == JOptionPane.YES_OPTION)
			{
				hideWindow();
			}			
		}
	}
	/**
	 * applySettingsListener is an inner class listener which holds two states which changes
	 * the mouse cursor as well as saving the settings made by calling writeSettings of the
	 * class FileLoader after setting the settings in the FileLoader class by calling 
	 * setters. Used by the label acceptSettingsLabel
	 * @author Robin E, Jakob L, Isac A, Cristian O, Eric P, Tobias W. 
	 *
	 */
	public class applySettingsListener extends MouseAdapter
	{
		FileLoader fileLoader = new FileLoader();
		public void mouseEntered(MouseEvent mentered)
		{
			Cursor c = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
			setCursor(c);
		}
		public void mouseExited(MouseEvent mexit)
		{
			Cursor c = Cursor.getDefaultCursor();
			setCursor(c);
		}
		public void mouseReleased(MouseEvent mreleased)
		{
			// Check which difficulty is selected.
			if(mediumDifficulty.isSelected())
			{
				fileLoader.setDifficulty("Medium");
			}
			else if(hardDifficulty.isSelected())
			{
				fileLoader.setDifficulty("Hard");
			}
			else if(extremeDifficulty.isSelected())
			{
				fileLoader.setDifficulty("Extreme");
			}
			else
			{
				fileLoader.setDifficulty("Easy");
			}
			
			// Check which theme is selected and fetch the words from that theme.
			if(themes.getSelectedItem() == "Animals")
			{
				fileLoader.setSelectedTheme("Animals");
			}
			else if(themes.getSelectedItem() == "Movies")
			{
				fileLoader.setSelectedTheme("Movies");
			}
			else
			{
				fileLoader.setSelectedTheme("Games");
			}
			
			// Check how many players are in the game.
			if(playerOne.isSelected())
			{
				fileLoader.setPlayersAmount("1");
			}
			else if(playerTwo.isSelected())
			{
				fileLoader.setPlayersAmount("2");
			}
			else if(playerThree.isSelected())
			{
				fileLoader.setPlayersAmount("3");
			}
			else
			{
				fileLoader.setPlayersAmount("1");
			}
			
			// Write the settings to the file.
			// and hide the window.
			fileLoader.writeSettings();
			sound.beepPool("hangman-sounds/beep01.wav");
			hideWindow();
		}
	}
}
